package top.aylan.org.ctrl.home;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.aylan.org.entity.Blog;
import top.aylan.org.entity.Project;
import top.aylan.org.service.ProjectService;
import top.aylan.org.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/blog")
public class BlogController {

    @Resource
    private ProjectService projectService;


    @RequestMapping(value = "/list")
    public String index(@RequestParam(value = "categoryId",defaultValue = "0")Integer categoryId,
                        @RequestParam(value = "tagId",defaultValue = "0")Integer tagId,
                        @RequestParam(value = "page",defaultValue = "1")Integer page,
                        ModelMap modelMap
                        ){
      modelMap.addAttribute("controllerName","blog");
      modelMap.addAttribute("actionName","index");

      Pager pager = new Pager<>();
      Project project = new Project();
      project.setStatus((byte)1);
      if(categoryId > 0){
          project.setCategoryId(categoryId);
      }
      pager.setCondition(project);
      pager.setCurrentPage(page);

      try {
          List<Blog> list = projectService.selectBlogsByPage(pager);
          pager.setList(list);
          modelMap.addAttribute("pager",pager);
      } catch (Exception e) {
            e.printStackTrace();
      }

      return  "/home/blogs";
    }


    @RequestMapping(value = "/details")
    public  String detail(
            @RequestParam(value = "id",defaultValue = "") Integer id,
            ModelMap model
    ){
        model.addAttribute("controllerName","blog");
        model.addAttribute("actionName","details");

        try {
            model.addAttribute("blog",projectService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/home/blog-detail";
    }
}
